package com.fahai.web.controller;

import com.fahai.common.PageVo;
import com.fahai.utils.json.JsonResult;
import com.fahai.web.entity.AllConfig;
import com.fahai.web.service.MarketService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * Created by Lilinjun on 17/5/22.
 */


@Controller
@EnableGlobalMethodSecurity(prePostEnabled = true)
@RequestMapping(value = "/market")
public class MarketController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(MarketController.class);

    @Autowired
    private MarketService marketService;


    /**
     * 跳转到查询页面
     *
     * @param
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','市场活动设定-0','市场活动设定-1')")
    @RequestMapping(value = "/market_rules", method = RequestMethod.GET)
    public String marketToSelect(Model model)
    {
        model.addAttribute("selectTab", "市场活动设定");
        return "market_rules";
    }

    /**
     * 市场活动设定查询
     *
     * @param pageVo
     * @param model
     * @return
     * @throws IOException
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','市场活动设定-0','市场活动设定-1')")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public JsonResult list(PageVo pageVo, Model model, AllConfig allConfig) throws IOException {
        pageVo = marketService.findAllConfig(allConfig, pageVo);
        model.addAttribute("marketVoList", pageVo);
        return ok("查询", pageVo);
    }


    /**
     * 编辑市场活动设定
     *
     * @param model
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','市场活动设定-0')")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult toupdateConfig( AllConfig allConfig, Model model) {
        marketService.updateAllConfig(allConfig);
        return ok("修改成功");
    }

    /**
     * 增加市场活动设定
     *
     * @param model
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','市场活动设定-0')")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addAllConfig( AllConfig allConfig, Model model) {
        marketService.insertAllConfig(allConfig);
        return ok("增加成功");
    }
}
